#pragma once
#include "mythread.hpp"
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include "Log.hpp"
using namespace ThreadMoudle;

const int defaultnum = 5;

template <class T>
class ThreadPool
{
private:
    ThreadPool(int thread_num = defaultnum)
        : _thread_num(thread_num), _sleep_thread_num(0), _isrunning(false)
    {
        pthread_mutex_init(&_mutex, nullptr);
        pthread_cond_init(&_cond, nullptr);
    }
    ~ThreadPool()
    {
        pthread_mutex_destroy(&_mutex);
        pthread_cond_destroy(&_cond);
    }
    ThreadPool(const ThreadPool<T> &t) = delete;
    ThreadPool &operator=(const ThreadPool<T> &t) = delete;

    void Init()
    {
        func_t func = std::bind(&ThreadPool::Hanldertask, this, std::placeholders::_1);
        for (int i = 0; i < _thread_num; i++)
        {
            std::string name = "thread-" + std::to_string(i + 1);
            _threads.emplace_back(name, func);
            LOG(DEBUG, "construct thread %s done, init success\n", name.c_str());
        }
    }

    void Start()
    {
        _isrunning = true;
        for (auto &e : _threads)
        {
            LOG(DEBUG, "start thread %s done.\n", e.Name().c_str());
            e.start();
        }
    }
    void Hanldertask(const std::string &name)
    {
        while (true)
        {
            LockQueue();
            while (IsEmpty() && _isrunning)
            {
                LOG(DEBUG, "%s is sleep begin\n", name.c_str());
                _sleep_thread_num++;
                Sleep();
                _sleep_thread_num--;
                LOG(DEBUG, "%s is wakeup\n", name.c_str());
            }
            if (IsEmpty() && !_isrunning)
            {
                LOG(DEBUG, "%s quit\n", name.c_str());
                UnlockQueue();
                break;
            }
            T t;
            t = _task_queue.front();
            _task_queue.pop();
            UnlockQueue();
            std::cout << name << ":";
            t();
            LOG(DEBUG, "%s is done\n", name.c_str());
        }
    }

public:
    void LockQueue()
    {
        pthread_mutex_lock(&_mutex);
    }
    void UnlockQueue()
    {
        pthread_mutex_unlock(&_mutex);
    }
    void Sleep()
    {
        pthread_cond_wait(&_cond, &_mutex);
    }
    void WakeUp()
    {
        pthread_cond_signal(&_cond);
    }
    void WakeUpAll()
    {
        pthread_cond_broadcast(&_cond);
    }
    bool IsEmpty()
    {
        return _task_queue.empty();
    }

public:
    static ThreadPool<T> *GetInstance()
    {
        if (_tp == nullptr)
        {
            pthread_mutex_lock(&_sig_mutex);
            if (_tp == nullptr)
            {
                _tp = new ThreadPool<T>;
                LOG(INFO, "creat threadpool\n");
                _tp->Init();
                _tp->Start();
            }
            else
            {
                LOG(INFO, "Get threadpool\n");
            }
             pthread_mutex_unlock(&_sig_mutex);
        }
        return _tp;
    }

    void Enqueue(T in)
    {
        LockQueue();
        if (_isrunning)
        {
            _task_queue.push(in);
            if (_sleep_thread_num > 0)
            {
                WakeUp();
            }
        }
        UnlockQueue();
    }
    void Stop()
    {
        LockQueue();
        _isrunning = false;
        WakeUpAll();
        UnlockQueue();
        LOG(DEBUG, "threadpool stop success\n");
    }

private:
    std::vector<thread> _threads;
    int _thread_num;
    std::queue<T> _task_queue; // 共享资源
    int _sleep_thread_num;
    bool _isrunning;
    pthread_mutex_t _mutex;
    pthread_cond_t _cond;
    static ThreadPool<T> *_tp;
    static pthread_mutex_t _sig_mutex;
};
template <class T>
ThreadPool<T> *ThreadPool<T>::_tp = nullptr;
template <class T>
pthread_mutex_t ThreadPool<T>::_sig_mutex= PTHREAD_MUTEX_INITIALIZER;